angular
  .module("jgappApp")
  .directive("searchInput", function($rootScope, DataService, Util) {
    return {
      scope: {
        showSearchInput: "=showSearchInput",
        handleSearch: "=handleSearch",
        apiurl: "=apiurl",
        title: "=title",
        param: "=param"
      },
      template:
        '<div class="tablestyle pickceritem" ng-show="showSearchInput">' +
        '<div class="tc lh35">{{title}}</div>' +
        '<div class="search_input_wrapper">' +
        '<input type="text" class="search_input" ng-model="searchinput" ng-model-options="{ debounce: 1000 }" ng-change="search()" placeholder="搜索">' +
        "</div>" +
        '<div class="search-result-list">' +
        '<div class="list-item" ng-repeat="item in data" ng-click="handleSearch(item)">{{item[0]}}[{{item[1]}}]</div>' +
        '<div class="list-item tc" ng-show="!data||data.length<1">无数据</div>' +
        "</div>" +
        "</div>",
      replace: true,
      restrict: "AE",
      link: function postLink(scope, element) {
        scope.search = function() {
          // 补充参数
          scope.param = scope.param || {};
          scope.param.query = scope.searchinput;
          scope.param.limit = 10;
          scope.param.type = 2;
          DataService.getData(scope.apiurl, scope.param).then(function(data) {
            scope.data = data;
          });
        };
        $rootScope.$on("closemasker", function() {
          // scope.data = [];
          scope.searchinput = "";
        });
        scope.$watch("apiurl", function(newvalue) {
          if (newvalue) {
            scope.search();
          }
        });
      }
    };
  });
